//
//  ServiceViewController.m
//  LoanMoney
//
//  Created by gynjobs on 2017/8/29.
//  Copyright © 2017年 cangming. All rights reserved.
//

#import "ServiceViewController.h"
#import "ServiceTableViewCell.h"
#import "CityInfo.h"
#import "CellModle.h"
#import "BaseAction.h"
#import "AfterWebViewController.h"
#define ARTICLE_CATE_LOAN_TIPS 60
@interface ServiceViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *DataArray;
@property(assign,nonatomic)NSInteger currentPage;
@end

@implementation ServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"攻略";
    [self addTableViewForSuperView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currentPage = 1;
        [self getTheDataFromNet:self.currentPage];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self updateData];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
- (void)addTableViewForSuperView{
    self.tableView = [[UITableView alloc]init];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = R_Y_H(220.0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
}
#pragma mark ---添加tableViewDataSource---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.DataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:serviceCellInd];
    if (cell==nil) {
        cell = [[ServiceTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:serviceCellInd];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.Modle = self.DataArray[indexPath.row];
    return cell;
}
#pragma mark ---tableViewDidSelect---
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CellModle *modle = self.DataArray[indexPath.row];
    if (![NSString isBlankStringWithString:modle.url]) {
        AfterWebViewController *zwebView = [[AfterWebViewController alloc]init];
        zwebView.webUrl = modle.url;
        zwebView.isCon = YES;
        zwebView.isHiddenTabbar = YES;
        [self.navigationController pushViewController:zwebView animated:YES];
    }
}
#pragma mark ---RequestNet---
-(void)updateData{
    [self getTheDataFromNet:self.currentPage];
}
-(void)getTheDataFromNet:(NSInteger)currentPage{
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    __weak typeof(self) weakSelf = self;
    if (currentPage==1) {
        [self.DataArray removeAllObjects];
    }
    [self getArticleListWithCate:ARTICLE_CATE_LOAN_TIPS andPage:currentPage andPageSize:10 withCallBack:^(id error_msg, id data) {
        if (![NSString isBlankStringWithString:error_msg]) {
            [LoadingAnimalView hideLoadingForView:weakSelf.view];
            return ;
        }
        NSMutableArray *dataArray = [NSMutableArray arrayWithArray:data];
        if (dataArray.count==0) {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.currentPage = weakSelf.currentPage+1;
        if (weakSelf.currentPage==0) {
            weakSelf.DataArray = [CellModle mj_objectArrayWithKeyValuesArray:data];
            [weakSelf.tableView reloadData];
        }else{
            
            [weakSelf.DataArray addObjectsFromArray:[CellModle mj_objectArrayWithKeyValuesArray:data]];
            [weakSelf.tableView reloadData];
        }
        [LoadingAnimalView hideLoadingForView:weakSelf.view];
    }];
}
- (void)getArticleListWithCate:(NSInteger)cate_id andPage:(NSInteger)page andPageSize:(NSInteger)page_size withCallBack:(ActionCallBackExtend)callBack{
    
    typeof (self)weakSelf = self;
    
    //获取资讯列表数据
    [[ClientApi getInstance] getDiscoverHotNewsInfoWithCateID:@"218" cityName:[[CityInfo getCacheCity] EN] page:weakSelf.currentPage withCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            GYNLog(@"error = %@",error.description);
        }else{
            GYNLog(@"success = %@",responseObject);
            NSArray *array = responseObject[@"items"];
            NSInteger sumCount = [responseObject[@"count"] integerValue];
            if (array) {
                if (self.currentPage * 10 > sumCount) {
                    callBack(nil,responseObject[@"items"]);
                }else{
                    callBack(nil,responseObject[@"items"]);
                }
            }
        }
    }];
}
/** 初始化数组 **/
-(NSMutableArray *)DataArray{
    if (_DataArray==nil) {
        _DataArray = [NSMutableArray array];
    }
    return _DataArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
